package com.turtle3d.formallanguage.symbol;

import java.util.Hashtable;

/**
 * L-System symbol, SymbolClass instance. Stores parameters.
 * 
 * @author Marek Paterczyk <marek.paterczyk@gmail.com>
 *
 */
public class Symbol {
	private String symbolName;
	private SymbolClass symbolClass;
	private int birthIteration=0;
		
	private Hashtable<String, Float> parameters = new Hashtable<String, Float>();
	
	public static String DISTANCE = "distance", TURN_ANGLE = "turnAngle",
			ROLL_ANGLE = "rollAngle", RADIUS = "radius",
			PITCH_ANGLE = "pitchAngle", VARIATION = "variation",
			SCALE_MULTIPLIER = "scaleMultiplier",
			SCALE_MULTIPLIER_X = "scaleMultiplierX",
			SCALE_MULTIPLIER_Y = "scaleMultiplierY",
			SCALE_MULTIPLIER_Z = "scaleMultiplierZ",
			SKIP_SYMBOLS = "skipSymbols";

	public Symbol(String symbolName) {
		super();
		this.symbolName = symbolName;
	}
	
	public void copyParametersFromSymbol(Symbol symbol) {
		for (String name : symbol.parameters.keySet()) {
			this.parameters.put(name, symbol.parameters.get(name));
		}
	}
	
	public Symbol(SymbolClass symbolClass)
	{
		super();
		this.symbolClass = symbolClass;
		symbolName = symbolClass.getSymbol();
	}

	public String toString(){
		if(parameters.isEmpty())
			return symbolName;
		
		String vars="";
		for(String key:parameters.keySet()){
			vars+=key+"="+parameters.get(key)+", ";			
		}
		vars=vars.substring(0, vars.length()-2);
				
		return symbolName+"("+vars+")";
	}
		
	@Override
	public int hashCode()
	{	
		//App.println("Hashcode: "+symbol+" -> "+symbol.hashCode());
		return symbolName.hashCode();
	}
	
	@Override
	public boolean equals(Object obj)
	{
		return symbolName.equals(((Symbol)obj).symbolName);
	}
	
	public void setParameter(String name, float value){
		parameters.put(name, value);
	}
	
	public Float getParameter(String name){				
		Float f = parameters.get(name);
		if(f==null){
			return SymbolFactory.getSymbolFactory().getParameter(name);
		}
		else
			return f;		
	}
	
	public Float getParameter(String name, float defaultValue){				
		return getParameter(name)!=null?getParameter(name):defaultValue;		
	}
	
	public boolean parameterExists(String name){
		return parameters.containsKey(name) || SymbolFactory.getSymbolFactory().getDefaultParameters().containsKey(name);
	}

	public SymbolClass getSymbolClass()
	{
		return symbolClass;
	}

	public String getSymbolName()
	{
		return symbolName;
	}

	public int getBirthIteration() {
		return birthIteration;
	}

	public void setBirthIteration(int birthIteration) {
		this.birthIteration = birthIteration;
	}
	
	public int getAge(){
		return getParameter("iterationsCount").intValue()-getBirthIteration();
	}
	
}
